package com.cup.bddm.impala.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.handler.Token;
import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.po.MchntBaseInfo;
import com.cup.bddm.impala.service.MchntService;
import com.cup.bddm.impala.vo.MchntDailyTransVo;
import com.cup.bddm.impala.vo.MchntInfoVo;
import com.cup.bddm.impala.vo.MchntInterTopVo;
import com.cup.bddm.impala.vo.MchntRankVo;
import com.cup.bddm.impala.vo.MchntRingGrowthVo;
import com.cup.bddm.impala.vo.MchntTopAppVo;
import com.cup.bddm.impala.vo.MchntTransVo;
import com.cup.bddm.impala.vo.MchntUpdateVo;
import com.cup.bddm.util.DateUtil;

@Token
@Controller("srv-MerchantController")
@RequestMapping(value = "/services/merchant")
public class MerchantController {

	static Logger logger = Logger.getLogger(MerchantController.class);

	@Autowired
	MchntService mchntService;

	@RequestMapping(value = "/abs/{mchntCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntInfoVo getMerchantInfo(@PathVariable("mchntCd") String mchntCd, @PathVariable("date") String date) {
		logger.info("merchant abstract info request:mchntCd=" + mchntCd);
		MchntInfoVo result = new MchntInfoVo();

		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantInfo(mchntCd, date);
		return result;
	}

	@RequestMapping(value = "/topmchnt/{mchntCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntInterTopVo getTopMchntInStreet(@PathVariable("mchntCd") String mchntCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("top merchant in street trans info request:mchntCd=" + mchntCd + ",type=" + type);

		MchntInterTopVo result = new MchntInterTopVo();
		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(type) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getTopMchntInStreet(mchntCd, type, date);
		return result;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public MchntUpdateVo updateMchntInfo(@RequestBody List<MchntBaseInfo> json) {
		logger.info("insert Mchnt info: size=" + json.size());
		MchntUpdateVo result = new MchntUpdateVo();

		if (json.size() == 0) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		result = mchntService.updateMchnts(json);

		return result;
	}

	@RequestMapping(value = "/trans/{mchntCd}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntTransVo getMerchantTransInfo(@PathVariable("mchntCd") String mchntCd, @PathVariable("date") String date) {
		logger.info("merchant trans info request:mchntCd=" + mchntCd + ",date=" + date);
		MchntTransVo result = new MchntTransVo();
		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantTransInfo(mchntCd, date);

		return result;
	}

	@RequestMapping(value = "/daily/{mchntCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntDailyTransVo getMerchantDailyTrans(@PathVariable("mchntCd") String mchntCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("merchant daily trans info request:mchntCd=" + mchntCd + ",type=" + type + ",date=" + date);
		MchntDailyTransVo result = new MchntDailyTransVo();
		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(type) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("all") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantDailyTrans(mchntCd, type, date);

		return result;
	}

	@RequestMapping(value = "/growth/{mchntCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntRingGrowthVo getMerchantRingGrowthInfo(@PathVariable("mchntCd") String mchntCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("merchant ring growth info request:mchntCd=" + mchntCd + ",type=" + type + ",date=" + date);
		MchntRingGrowthVo result = new MchntRingGrowthVo();
		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(type) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("all") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantRingGrowthInfo(mchntCd, type, date);

		return result;
	}

	@RequestMapping(value = "/topapp/{mchntCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntTopAppVo getMerchantTopAppInfo(@PathVariable("mchntCd") String mchntCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("merchant top app info request:mchntCd=" + mchntCd + ",type=" + type + ",date=" + date);
		MchntTopAppVo result = new MchntTopAppVo();
		if (StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(type) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantTopAppInfo(mchntCd, type, date);

		return result;
	}

	@RequestMapping(value = "/rank/{streetCd}/{mchntCd}/{type}/{date}", method = RequestMethod.GET)
	@ResponseBody
	public MchntRankVo getMerchantRankInfo(@PathVariable("streetCd") String streetCd, @PathVariable("mchntCd") String mchntCd, @PathVariable("type") String type, @PathVariable("date") String date) {
		logger.info("merchant rank info request:mchntCd=" + mchntCd + ",type=" + type + ",date=" + date);
		MchntRankVo result = new MchntRankVo();
		if (StringUtils.isEmpty(streetCd) || StringUtils.isEmpty(mchntCd) || StringUtils.isEmpty(type) || StringUtils.isEmpty(date)) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!type.equals("nfc") && !type.equals("qrc") && !type.equals("offline")) {
			result.setState(Constant.PARA_ERROR);
			return result;
		}

		if (!DateUtil.isValidDate(date)) {
			result.setState(Constant.DATE_FORMAT_ERROR);
			return result;
		}

		result = mchntService.getMerchantRankInfo(streetCd, mchntCd, type, date);

		return result;
	}

}
